#!/bin/sh

# BASEDIR=/home/tte/c30_peripheral/src
BASEDIR=/home/tte/.hudson/jobs/dsPIC_Plib_Build_Workspace/workspace/src

RETCODE=0
PIC30_CD=/home/tte/install-linux-exlm
  
ECHO=`which echo`
if [ ! -x $ECHO ]; then
  echo "******* echo might be broken!"
  ECHO=echo
fi

echo "__C30_VERSION__" > version.c
VERSION=`${PIC30_CD}/bin/pic30-gcc -E version.c | grep -v "#" | sed 's/[^0-9]//g'`
REV_A2_SUPPORTED=no
OPTS="-msmall-code -mconst-in-data -Os -I../../../include -mtext=.libperi"

if [ "$VERSION" -gt "310" ]; then
  OPTS="$OPTS -mno-file"
fi

files=`ls $BASEDIR/pmc/$PERIPHERAL/*.c`;

for REV in "A2" ""; do
  if [ "$REV" = "A2" ]; then
    if [ "${REV_A2_SUPPORTED}" = "no" ]; then continue; fi
    # enumerate non A2 parts :(
    case ${PROCESSOR} in
      30F5015) continue;;
    esac
  fi
  for f in $files; do
    ${ECHO} ${PIC30_CD}/bin/pic30-gcc -c -mcpu=${PROCESSOR}${REV} $OPTS -D__dsPIC${PROCESSOR}${REV}__ $f
    ${PIC30_CD}/bin/pic30-gcc -c -mcpu=${PROCESSOR}${REV} $OPTS -D__dsPIC${PROCESSOR}${REV}__ $f > .output 2>&1
    if [ $? -ne 0 ] ; then
      if ( grep -q "Does not build on this target" .output ); then
	continue;
      fi
      RETCODE=1
    fi
    cat .output
  done
done
exit $RETCODE

